package com.luuyi.actionchain.action;

import android.content.Context;
import android.os.Handler;

public class AsyncDelayAction extends AbsAction {

    public static final long DEFAULT_DELAY_TIME = 1500L;

    private Context context;

    private long delayTime = DEFAULT_DELAY_TIME;

    public AsyncDelayAction(Context context) {
        this.context = context;
        setIsSyncAction(false);
    }

    public AsyncDelayAction setDelay(long delayTime) {
        this.delayTime = delayTime;
        return this;
    }

    public long getDelayTime() {
        return delayTime;
    }

    @Override
    public void handle() {
        if (delayTime <= 0) {
            doNext();
            return;
        }
        new Handler(context.getMainLooper()).postDelayed(this::doNext, delayTime);
    }
}
